const WXAPI = require('../../wxapi/domain')
const CONFIG = require('../../config.js')
const app = getApp()

Page({
  data: {
    action: '',
    flag: 0,
    nickNameList:null
  },
  onShow(){
    const openid = wx.getStorageSync('openid');
    if (!openid) {
      app.navigateToLogin = false
      app.goLoginPageTimeOut()
      return
    }
    const _this = this
    const data = { "openId": openid, "id": _this.options.id }
    WXAPI.queryAction(data).then(res => {
      if (res.code === 0) {
        _this.setData({
          action: res.data,
          nickNameList: res.data.nickNameList,
          flag: res.data.flag
        })
      }
    })
  },
  swiperchange: function(e) { // banner滚动事件
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  
  onLoad: function(e) {
   
  },
  onPageScroll(e) {
    let scrollTop = this.data.scrollTop
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  

  onShareAppMessage: function() {
    return {
      title: wx.getStorageSync('mallName'),
      path: '/pages/index/index?inviter_id=' + wx.getStorageSync('uid'),
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },
  // 以下为搜索框事件
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  goMap() { // 打开地图
    wx.openLocation({
      latitude: this.data.action.latitude,
      longitude: this.data.action.longitude,
      name: this.data.action.name,
      address: this.data.action.address
    })
  },
  callPhone(e){
    const tel = e.currentTarget.dataset.tel
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  joinAction() {
    const openid = wx.getStorageSync('openid');
    if (!openid) {
      app.navigateToLogin = false
      app.goLoginPageTimeOut()
      return
    }
    const _this = this
    const data = { "openId": openid, "id": _this.data.action.id }
    WXAPI.joinAction(data).then(res => {
      if (res.code === 0) {
        _this.setData({
          action: res.data,
          nickNameList: res.data.nickNameList,
          flag: res.data.flag
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  cancelAction() {
    const _this = this
    const openid = wx.getStorageSync('openid');
    const data = { "openId": openid, "id": _this.data.action.id }
    WXAPI.cancelAction(data).then(res => {
      if (res.code === 0) {
        _this.setData({
          action: res.data,
          nickNameList: res.data.nickNameList,
          flag: res.data.flag
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  }
})